home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / p_man / cat3 / perl5 / DB_File.z / DB_File
Encoding:
Text File  |  2002-10-03  |  59.7 KB  |  1,783 lines

  1.  
  2.  
  3.  
  4. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      DB_File - Perl5 access to Berkeley DB version 1.x
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.       use DB_File ;
  13.  
  14.       [$X =] tie %hash,  'DB_File', [$filename, $flags, $mode, $DB_HASH] ;
  15.       [$X =] tie %hash,  'DB_File', $filename, $flags, $mode, $DB_BTREE ;
  16.       [$X =] tie @array, 'DB_File', $filename, $flags, $mode, $DB_RECNO ;
  17.  
  18.       $status = $X->del($key [, $flags]) ;
  19.       $status = $X->put($key, $value [, $flags]) ;
  20.       $status = $X->get($key, $value [, $flags]) ;
  21.       $status = $X->seq($key, $value, $flags) ;
  22.       $status = $X->sync([$flags]) ;
  23.       $status = $X->fd ;
  24.  
  25.       # BTREE only
  26.       $count = $X->get_dup($key) ;
  27.       @list  = $X->get_dup($key) ;
  28.       %list  = $X->get_dup($key, 1) ;
  29.  
  30.       # RECNO only
  31.       $a = $X->length;
  32.       $a = $X->pop ;
  33.       $X->push(list);
  34.       $a = $X->shift;
  35.       $X->unshift(list);
  36.  
  37.       untie %hash ;
  38.       untie @array ;
  39.  
  40.  
  41. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  42.      DDDDBBBB____FFFFiiiilllleeee is a module which allows Perl programs to make use of the
  43.      facilities provided by Berkeley DB version 1.x (if you have a newer
  44.      version of DB, see the section on _U_s_i_n_g _D_B__F_i_l_e _w_i_t_h _B_e_r_k_e_l_e_y _D_B _v_e_r_s_i_o_n
  45.      _2). It is assumed that you have a copy of the Berkeley DB manual pages at
  46.      hand when reading this documentation. The interface defined here mirrors
  47.      the Berkeley DB interface closely.
  48.  
  49.      Berkeley DB is a C library which provides a consistent interface to a
  50.      number of database formats.  DDDDBBBB____FFFFiiiilllleeee provides an interface to all three
  51.      of the database types currently supported by Berkeley DB.
  52.  
  53.      The file types are:
  54.  
  55.      DDDDBBBB____HHHHAAAASSSSHHHH
  56.           This database type allows arbitrary key/value pairs to be stored in
  57.           data files. This is equivalent to the functionality provided by
  58.           other hashing packages like DBM, NDBM, ODBM, GDBM, and SDBM.
  59.           Remember though, the files created using DB_HASH are not compatible
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  71.  
  72.  
  73.  
  74.           with any of the other packages mentioned.
  75.  
  76.           A default hashing algorithm, which will be adequate for most
  77.           applications, is built into Berkeley DB. If you do need to use your
  78.           own hashing algorithm it is possible to write your own in Perl and
  79.           have DDDDBBBB____FFFFiiiilllleeee use it instead.
  80.  
  81.      DDDDBBBB____BBBBTTTTRRRREEEEEEEE
  82.           The btree format allows arbitrary key/value pairs to be stored in a
  83.           sorted, balanced binary tree.
  84.  
  85.           As with the DB_HASH format, it is possible to provide a user defined
  86.           Perl routine to perform the comparison of keys. By default, though,
  87.           the keys are stored in lexical order.
  88.  
  89.      DDDDBBBB____RRRREEEECCCCNNNNOOOO
  90.           DB_RECNO allows both fixed-length and variable-length flat text
  91.           files to be manipulated using the same key/value pair interface as
  92.           in DB_HASH and DB_BTREE.  In this case the key will consist of a
  93.           record (line) number.
  94.  
  95.      UUUUssssiiiinnnngggg DDDDBBBB____FFFFiiiilllleeee wwwwiiiitttthhhh BBBBeeeerrrrkkkkeeeelllleeeeyyyy DDDDBBBB vvvveeeerrrrssssiiiioooonnnn 2222
  96.  
  97.      Although DDDDBBBB____FFFFiiiilllleeee is intended to be used with Berkeley DB version 1, it
  98.      can also be used with version 2. In this case the interface is limited to
  99.      the functionality provided by Berkeley DB 1.x. Anywhere the version 2
  100.      interface differs, DDDDBBBB____FFFFiiiilllleeee arranges for it to work like version 1. This
  101.      feature allows DDDDBBBB____FFFFiiiilllleeee scripts that were built with version 1 to be
  102.      migrated to version 2 without any changes.
  103.  
  104.      If you want to make use of the new features available in Berkeley DB 2.x,
  105.      use the Perl module BBBBeeeerrrrkkkkeeeelllleeeeyyyyDDDDBBBB instead.
  106.  
  107.      At the time of writing this document the BBBBeeeerrrrkkkkeeeelllleeeeyyyyDDDDBBBB module is still alpha
  108.      quality (the version number is < 1.0), and so unsuitable for use in any
  109.      serious development work. Once its version number is >= 1.0, it is
  110.      considered stable enough for real work.
  111.  
  112.      NNNNooootttteeee:::: The database file format has changed in Berkeley DB version 2.  If
  113.      you cannot recreate your databases, you must dump any existing databases
  114.      with the db_dump185 utility that comes with Berkeley DB.  Once you have
  115.      upgraded DB_File to use Berkeley DB version 2, your databases can be
  116.      recreated using db_load. Refer to the Berkeley DB documentation for
  117.      further details.
  118.  
  119.      Please read the _C_O_P_Y_R_I_G_H_T manpage before using version 2.x of Berkeley DB
  120.      with DB_File.
  121.  
  122.      IIIInnnntttteeeerrrrffffaaaacccceeee ttttoooo BBBBeeeerrrrkkkkeeeelllleeeeyyyy DDDDBBBB
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  137.  
  138.  
  139.  
  140.      DDDDBBBB____FFFFiiiilllleeee allows access to Berkeley DB files using the _t_i_e() mechanism in
  141.      Perl 5 (for full details, see the tie() entry in the _p_e_r_l_f_u_n_c manpage).
  142.      This facility allows DDDDBBBB____FFFFiiiilllleeee to access Berkeley DB files using either an
  143.      associative array (for DB_HASH & DB_BTREE file types) or an ordinary
  144.      array (for the DB_RECNO file type).
  145.  
  146.      In addition to the _t_i_e() interface, it is also possible to access most of
  147.      the functions provided in the Berkeley DB API directly.  See the section
  148.      on _T_H_E _A_P_I _I_N_T_E_R_F_A_C_E.
  149.  
  150.      OOOOppppeeeennnniiiinnnngggg aaaa BBBBeeeerrrrkkkkeeeelllleeeeyyyy DDDDBBBB DDDDaaaattttaaaabbbbaaaasssseeee FFFFiiiilllleeee
  151.  
  152.      Berkeley DB uses the function _d_b_o_p_e_n() to open or create a database.
  153.      Here is the C prototype for _d_b_o_p_e_n():
  154.  
  155.            DB*
  156.            dbopen (const char * file, int flags, int mode,
  157.                    DBTYPE type, const void * openinfo)
  158.  
  159.      The parameter type is an enumeration which specifies which of the 3
  160.      interface methods (DB_HASH, DB_BTREE or DB_RECNO) is to be used.
  161.      Depending on which of these is actually chosen, the final parameter,
  162.      _o_p_e_n_i_n_f_o points to a data structure which allows tailoring of the
  163.      specific interface method.
  164.  
  165.      This interface is handled slightly differently in DDDDBBBB____FFFFiiiilllleeee. Here is an
  166.      equivalent call using DDDDBBBB____FFFFiiiilllleeee:
  167.  
  168.              tie %array, 'DB_File', $filename, $flags, $mode, $DB_HASH ;
  169.  
  170.      The filename, flags and mode parameters are the direct equivalent of
  171.      their _d_b_o_p_e_n() counterparts. The final parameter $DB_HASH performs the
  172.      function of both the type and openinfo parameters in _d_b_o_p_e_n().
  173.  
  174.      In the example above $DB_HASH is actually a pre-defined reference to a
  175.      hash object. DDDDBBBB____FFFFiiiilllleeee has three of these pre-defined references.  Apart
  176.      from $DB_HASH, there is also $DB_BTREE and $DB_RECNO.
  177.  
  178.      The keys allowed in each of these pre-defined references is limited to
  179.      the names used in the equivalent C structure. So, for example, the
  180.      $DB_HASH reference will only allow keys called bsize, cachesize, ffactor,
  181.      hash, lorder and nelem.
  182.  
  183.      To change one of these elements, just assign to it like this:
  184.  
  185.              $DB_HASH->{'cachesize'} = 10000 ;
  186.  
  187.      The three predefined variables $DB_HASH, $DB_BTREE and $DB_RECNO are
  188.      usually adequate for most applications.  If you do need to create extra
  189.      instances of these objects, constructors are available for each file
  190.      type.
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  203.  
  204.  
  205.  
  206.      Here are examples of the constructors and the valid options available for
  207.      DB_HASH, DB_BTREE and DB_RECNO respectively.
  208.  
  209.           $a = new DB_File::HASHINFO ;
  210.           $a->{'bsize'} ;
  211.           $a->{'cachesize'} ;
  212.           $a->{'ffactor'};
  213.           $a->{'hash'} ;
  214.           $a->{'lorder'} ;
  215.           $a->{'nelem'} ;
  216.  
  217.           $b = new DB_File::BTREEINFO ;
  218.           $b->{'flags'} ;
  219.           $b->{'cachesize'} ;
  220.           $b->{'maxkeypage'} ;
  221.           $b->{'minkeypage'} ;
  222.           $b->{'psize'} ;
  223.           $b->{'compare'} ;
  224.           $b->{'prefix'} ;
  225.           $b->{'lorder'} ;
  226.  
  227.           $c = new DB_File::RECNOINFO ;
  228.           $c->{'bval'} ;
  229.           $c->{'cachesize'} ;
  230.           $c->{'psize'} ;
  231.           $c->{'flags'} ;
  232.           $c->{'lorder'} ;
  233.           $c->{'reclen'} ;
  234.           $c->{'bfname'} ;
  235.  
  236.      The values stored in the hashes above are mostly the direct equivalent of
  237.      their C counterpart. Like their C counterparts, all are set to a default
  238.      values - that means you don't have to set _a_l_l of the values when you only
  239.      want to change one. Here is an example:
  240.  
  241.           $a = new DB_File::HASHINFO ;
  242.           $a->{'cachesize'} =  12345 ;
  243.           tie %y, 'DB_File', "filename", $flags, 0777, $a ;
  244.  
  245.      A few of the options need extra discussion here. When used, the C
  246.      equivalent of the keys hash, compare and prefix store pointers to C
  247.      functions. In DDDDBBBB____FFFFiiiilllleeee these keys are used to store references to Perl
  248.      subs. Below are templates for each of the subs:
  249.  
  250.          sub hash
  251.          {
  252.              my ($data) = @_ ;
  253.              ...
  254.              # return the hash value for $data
  255.              return $hash ;
  256.          }
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  269.  
  270.  
  271.  
  272.          sub compare
  273.          {
  274.              my ($key, $key2) = @_ ;
  275.              ...
  276.              # return  0 if $key1 eq $key2
  277.              #        -1 if $key1 lt $key2
  278.              #         1 if $key1 gt $key2
  279.              return (-1 , 0 or 1) ;
  280.          }
  281.  
  282.          sub prefix
  283.          {
  284.              my ($key, $key2) = @_ ;
  285.              ...
  286.              # return number of bytes of $key2 which are
  287.              # necessary to determine that it is greater than $key1
  288.              return $bytes ;
  289.          }
  290.  
  291.      See the section on _C_h_a_n_g_i_n_g _t_h_e _B_T_R_E_E _s_o_r_t _o_r_d_e_r for an example of using
  292.      the compare template.
  293.  
  294.      If you are using the DB_RECNO interface and you intend making use of
  295.      bval, you should check out the section on _T_h_e '_b_v_a_l' _O_p_t_i_o_n.
  296.  
  297.      DDDDeeeeffffaaaauuuulllltttt PPPPaaaarrrraaaammmmeeeetttteeeerrrrssss
  298.  
  299.      It is possible to omit some or all of the final 4 parameters in the call
  300.      to tie and let them take default values. As DB_HASH is the most common
  301.      file format used, the call:
  302.  
  303.          tie %A, "DB_File", "filename" ;
  304.  
  305.      is equivalent to:
  306.  
  307.          tie %A, "DB_File", "filename", O_CREAT|O_RDWR, 0666, $DB_HASH ;
  308.  
  309.      It is also possible to omit the filename parameter as well, so the call:
  310.  
  311.          tie %A, "DB_File" ;
  312.  
  313.      is equivalent to:
  314.  
  315.          tie %A, "DB_File", undef, O_CREAT|O_RDWR, 0666, $DB_HASH ;
  316.  
  317.      See the section on _I_n _M_e_m_o_r_y _D_a_t_a_b_a_s_e_s for a discussion on the use of
  318.      undef in place of a filename.
  319.  
  320.      IIIInnnn MMMMeeeemmmmoooorrrryyyy DDDDaaaattttaaaabbbbaaaasssseeeessss
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  335.  
  336.  
  337.  
  338.      Berkeley DB allows the creation of in-memory databases by using NULL
  339.      (that is, a (char *)0 in C) in place of the filename.  DDDDBBBB____FFFFiiiilllleeee uses undef
  340.      instead of NULL to provide this functionality.
  341.  
  342. DDDDBBBB____HHHHAAAASSSSHHHH
  343.      The DB_HASH file format is probably the most commonly used of the three
  344.      file formats that DDDDBBBB____FFFFiiiilllleeee supports. It is also very straightforward to
  345.      use.
  346.  
  347.      AAAA SSSSiiiimmmmpppplllleeee EEEExxxxaaaammmmpppplllleeee
  348.  
  349.      This example shows how to create a database, add key/value pairs to the
  350.      database, delete keys/value pairs and finally how to enumerate the
  351.      contents of the database.
  352.  
  353.          use strict ;
  354.          use DB_File ;
  355.          use vars qw( %h $k $v ) ;
  356.  
  357.          tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH
  358.              or die "Cannot open file 'fruit': $!\n";
  359.  
  360.          # Add a few key/value pairs to the file
  361.          $h{"apple"} = "red" ;
  362.          $h{"orange"} = "orange" ;
  363.          $h{"banana"} = "yellow" ;
  364.          $h{"tomato"} = "red" ;
  365.  
  366.          # Check for existence of a key
  367.          print "Banana Exists\n\n" if $h{"banana"} ;
  368.  
  369.          # Delete a key/value pair.
  370.          delete $h{"apple"} ;
  371.  
  372.          # print the contents of the file
  373.          while (($k, $v) = each %h)
  374.            { print "$k -> $v\n" }
  375.  
  376.          untie %h ;
  377.  
  378.      here is the output:
  379.  
  380.          Banana Exists
  381.  
  382.          orange -> orange
  383.          tomato -> red
  384.          banana -> yellow
  385.  
  386.      Note that the like ordinary associative arrays, the order of the keys
  387.      retrieved is in an apparently random order.
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  401.  
  402.  
  403.  
  404. DDDDBBBB____BBBBTTTTRRRREEEEEEEE
  405.      The DB_BTREE format is useful when you want to store data in a given
  406.      order. By default the keys will be stored in lexical order, but as you
  407.      will see from the example shown in the next section, it is very easy to
  408.      define your own sorting function.
  409.  
  410.      CCCChhhhaaaannnnggggiiiinnnngggg tttthhhheeee BBBBTTTTRRRREEEEEEEE ssssoooorrrrtttt oooorrrrddddeeeerrrr
  411.  
  412.      This script shows how to override the default sorting algorithm that
  413.      BTREE uses. Instead of using the normal lexical ordering, a case
  414.      insensitive compare function will be used.
  415.  
  416.          use strict ;
  417.          use DB_File ;
  418.  
  419.          my %h ;
  420.  
  421.          sub Compare
  422.          {
  423.              my ($key1, $key2) = @_ ;
  424.              "\L$key1" cmp "\L$key2" ;
  425.          }
  426.  
  427.          # specify the Perl sub that will do the comparison
  428.          $DB_BTREE->{'compare'} = \&Compare ;
  429.  
  430.          tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE
  431.              or die "Cannot open file 'tree': $!\n" ;
  432.  
  433.          # Add a key/value pair to the file
  434.          $h{'Wall'} = 'Larry' ;
  435.          $h{'Smith'} = 'John' ;
  436.          $h{'mouse'} = 'mickey' ;
  437.          $h{'duck'}  = 'donald' ;
  438.  
  439.          # Delete
  440.          delete $h{"duck"} ;
  441.  
  442.          # Cycle through the keys printing them in order.
  443.          # Note it is not necessary to sort the keys as
  444.          # the btree will have kept them in order automatically.
  445.          foreach (keys %h)
  446.            { print "$_\n" }
  447.  
  448.          untie %h ;
  449.  
  450.      Here is the output from the code above.
  451.  
  452.          mouse
  453.          Smith
  454.          Wall
  455.  
  456.  
  457.  
  458.  
  459.                                                                         PPPPaaaaggggeeee 7777
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  467.  
  468.  
  469.  
  470.      There are a few point to bear in mind if you want to change the ordering
  471.      in a BTREE database:
  472.  
  473.      1.   The new compare function must be specified when you create the
  474.           database.
  475.  
  476.      2.   You cannot change the ordering once the database has been created.
  477.           Thus you must use the same compare function every time you access
  478.           the database.
  479.  
  480.      HHHHaaaannnnddddlllliiiinnnngggg DDDDuuuupppplllliiiiccccaaaatttteeee KKKKeeeeyyyyssss
  481.  
  482.      The BTREE file type optionally allows a single key to be associated with
  483.      an arbitrary number of values. This option is enabled by setting the
  484.      flags element of $DB_BTREE to R_DUP when creating the database.
  485.  
  486.      There are some difficulties in using the tied hash interface if you want
  487.      to manipulate a BTREE database with duplicate keys. Consider this code:
  488.  
  489.          use strict ;
  490.          use DB_File ;
  491.  
  492.          use vars qw($filename %h ) ;
  493.  
  494.          $filename = "tree" ;
  495.          unlink $filename ;
  496.  
  497.          # Enable duplicate records
  498.          $DB_BTREE->{'flags'} = R_DUP ;
  499.  
  500.          tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
  501.              or die "Cannot open $filename: $!\n";
  502.  
  503.          # Add some key/value pairs to the file
  504.          $h{'Wall'} = 'Larry' ;
  505.          $h{'Wall'} = 'Brick' ; # Note the duplicate key
  506.          $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
  507.          $h{'Smith'} = 'John' ;
  508.          $h{'mouse'} = 'mickey' ;
  509.  
  510.          # iterate through the associative array
  511.          # and print each key/value pair.
  512.          foreach (keys %h)
  513.            { print "$_  -> $h{$_}\n" }
  514.  
  515.          untie %h ;
  516.  
  517.      Here is the output:
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.                                                                         PPPPaaaaggggeeee 8888
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  533.  
  534.  
  535.  
  536.          Smith   -> John
  537.          Wall    -> Larry
  538.          Wall    -> Larry
  539.          Wall    -> Larry
  540.          mouse   -> mickey
  541.  
  542.      As you can see 3 records have been successfully created with key Wall -
  543.      the only thing is, when they are retrieved from the database they _s_e_e_m to
  544.      have the same value, namely Larry. The problem is caused by the way that
  545.      the associative array interface works. Basically, when the associative
  546.      array interface is used to fetch the value associated with a given key,
  547.      it will only ever retrieve the first value.
  548.  
  549.      Although it may not be immediately obvious from the code above, the
  550.      associative array interface can be used to write values with duplicate
  551.      keys, but it cannot be used to read them back from the database.
  552.  
  553.      The way to get around this problem is to use the Berkeley DB API method
  554.      called seq.  This method allows sequential access to key/value pairs. See
  555.      the section on _T_H_E _A_P_I _I_N_T_E_R_F_A_C_E for details of both the seq method and
  556.      the API in general.
  557.  
  558.      Here is the script above rewritten using the seq API method.
  559.  
  560.          use strict ;
  561.          use DB_File ;
  562.  
  563.          use vars qw($filename $x %h $status $key $value) ;
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.                                                                         PPPPaaaaggggeeee 9999
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  599.  
  600.  
  601.  
  602.          $filename = "tree" ;
  603.          unlink $filename ;
  604.  
  605.          # Enable duplicate records
  606.          $DB_BTREE->{'flags'} = R_DUP ;
  607.  
  608.          $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
  609.              or die "Cannot open $filename: $!\n";
  610.  
  611.          # Add some key/value pairs to the file
  612.          $h{'Wall'} = 'Larry' ;
  613.          $h{'Wall'} = 'Brick' ; # Note the duplicate key
  614.          $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
  615.          $h{'Smith'} = 'John' ;
  616.          $h{'mouse'} = 'mickey' ;
  617.  
  618.          # iterate through the btree using seq
  619.          # and print each key/value pair.
  620.          $key = $value = 0 ;
  621.          for ($status = $x->seq($key, $value, R_FIRST) ;
  622.               $status == 0 ;
  623.               $status = $x->seq($key, $value, R_NEXT) )
  624.            {  print "$key -> $value\n" }
  625.  
  626.          undef $x ;
  627.          untie %h ;
  628.  
  629.      that prints:
  630.  
  631.          Smith   -> John
  632.          Wall    -> Brick
  633.          Wall    -> Brick
  634.          Wall    -> Larry
  635.          mouse   -> mickey
  636.  
  637.      This time we have got all the key/value pairs, including the multiple
  638.      values associated with the key Wall.
  639.  
  640.      TTTThhhheeee _g_e_t__d_u_p() Method
  641.  
  642.      DDDDBBBB____FFFFiiiilllleeee comes with a utility method, called get_dup, to assist in reading
  643.      duplicate values from BTREE databases. The method can take the following
  644.      forms:
  645.  
  646.          $count = $x->get_dup($key) ;
  647.          @list  = $x->get_dup($key) ;
  648.          %list  = $x->get_dup($key, 1) ;
  649.  
  650.      In a scalar context the method returns the number of values associated
  651.      with the key, $key.
  652.  
  653.  
  654.  
  655.  
  656.  
  657.                                                                        PPPPaaaaggggeeee 11110000
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  665.  
  666.  
  667.  
  668.      In list context, it returns all the values which match $key. Note that
  669.      the values will be returned in an apparently random order.
  670.  
  671.      In list context, if the second parameter is present and evaluates TRUE,
  672.      the method returns an associative array. The keys of the associative
  673.      array correspond to the values that matched in the BTREE and the values
  674.      of the array are a count of the number of times that particular value
  675.      occurred in the BTREE.
  676.  
  677.      So assuming the database created above, we can use get_dup like this:
  678.  
  679.          my $cnt  = $x->get_dup("Wall") ;
  680.          print "Wall occurred $cnt times\n" ;
  681.  
  682.          my %hash = $x->get_dup("Wall", 1) ;
  683.          print "Larry is there\n" if $hash{'Larry'} ;
  684.          print "There are $hash{'Brick'} Brick Walls\n" ;
  685.  
  686.          my @list = $x->get_dup("Wall") ;
  687.          print "Wall =>      [@list]\n" ;
  688.  
  689.          @list = $x->get_dup("Smith") ;
  690.          print "Smith =>     [@list]\n" ;
  691.  
  692.          @list = $x->get_dup("Dog") ;
  693.          print "Dog =>       [@list]\n" ;
  694.  
  695.      and it will print:
  696.  
  697.          Wall occurred 3 times
  698.          Larry is there
  699.          There are 2 Brick Walls
  700.          Wall =>     [Brick Brick Larry]
  701.          Smith =>    [John]
  702.          Dog =>      []
  703.  
  704.  
  705.      MMMMaaaattttcccchhhhiiiinnnngggg PPPPaaaarrrrttttiiiiaaaallll KKKKeeeeyyyyssss
  706.  
  707.      The BTREE interface has a feature which allows partial keys to be
  708.      matched. This functionality is _o_n_l_y available when the seq method is used
  709.      along with the R_CURSOR flag.
  710.  
  711.          $x->seq($key, $value, R_CURSOR) ;
  712.  
  713.      Here is the relevant quote from the dbopen man page where it defines the
  714.      use of the R_CURSOR flag with seq:
  715.  
  716.          Note, for the DB_BTREE access method, the returned key is not
  717.          necessarily an exact match for the specified key. The returned key
  718.          is the smallest key greater than or equal to the specified key,
  719.          permitting partial key matches and range searches.
  720.  
  721.  
  722.  
  723.                                                                        PPPPaaaaggggeeee 11111111
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  731.  
  732.  
  733.  
  734.      In the example script below, the match sub uses this feature to find and
  735.      print the first matching key/value pair given a partial key.
  736.  
  737.          use strict ;
  738.          use DB_File ;
  739.          use Fcntl ;
  740.  
  741.          use vars qw($filename $x %h $st $key $value) ;
  742.  
  743.          sub match
  744.          {
  745.              my $key = shift ;
  746.              my $value = 0;
  747.              my $orig_key = $key ;
  748.              $x->seq($key, $value, R_CURSOR) ;
  749.              print "$orig_key\t-> $key\t-> $value\n" ;
  750.          }
  751.  
  752.          $filename = "tree" ;
  753.          unlink $filename ;
  754.  
  755.          $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
  756.              or die "Cannot open $filename: $!\n";
  757.  
  758.          # Add some key/value pairs to the file
  759.          $h{'mouse'} = 'mickey' ;
  760.          $h{'Wall'} = 'Larry' ;
  761.          $h{'Walls'} = 'Brick' ;
  762.          $h{'Smith'} = 'John' ;
  763.  
  764.  
  765.          $key = $value = 0 ;
  766.          print "IN ORDER\n" ;
  767.          for ($st = $x->seq($key, $value, R_FIRST) ;
  768.               $st == 0 ;
  769.               $st = $x->seq($key, $value, R_NEXT) )
  770.  
  771.            {  print "$key -> $value\n" }
  772.  
  773.          print "\nPARTIAL MATCH\n" ;
  774.  
  775.          match "Wa" ;
  776.          match "A" ;
  777.          match "a" ;
  778.  
  779.          undef $x ;
  780.          untie %h ;
  781.  
  782.      Here is the output:
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.                                                                        PPPPaaaaggggeeee 11112222
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  797.  
  798.  
  799.  
  800.          IN ORDER
  801.          Smith -> John
  802.          Wall  -> Larry
  803.          Walls -> Brick
  804.          mouse -> mickey
  805.  
  806.          PARTIAL MATCH
  807.          Wa -> Wall  -> Larry
  808.          A  -> Smith -> John
  809.          a  -> mouse -> mickey
  810.  
  811.  
  812. DDDDBBBB____RRRREEEECCCCNNNNOOOO
  813.      DB_RECNO provides an interface to flat text files. Both variable and
  814.      fixed length records are supported.
  815.  
  816.      In order to make RECNO more compatible with Perl the array offset for all
  817.      RECNO arrays begins at 0 rather than 1 as in Berkeley DB.
  818.  
  819.      As with normal Perl arrays, a RECNO array can be accessed using negative
  820.      indexes. The index -1 refers to the last element of the array, -2 the
  821.      second last, and so on. Attempting to access an element before the start
  822.      of the array will raise a fatal run-time error.
  823.  
  824.      TTTThhhheeee ''''bbbbvvvvaaaallll'''' OOOOppppttttiiiioooonnnn
  825.  
  826.      The operation of the bval option warrants some discussion. Here is the
  827.      definition of bval from the Berkeley DB 1.85 recno manual page:
  828.  
  829.          The delimiting byte to be used to mark  the  end  of  a
  830.          record for variable-length records, and the pad charac-
  831.          ter for fixed-length records.  If no  value  is  speci-
  832.          fied,  newlines  (``\n'')  are  used to mark the end of
  833.          variable-length records and  fixed-length  records  are
  834.          padded with spaces.
  835.  
  836.      The second sentence is wrong. In actual fact bval will only default to
  837.      "\n" when the openinfo parameter in dbopen is NULL. If a non-NULL
  838.      openinfo parameter is used at all, the value that happens to be in bval
  839.      will be used. That means you always have to specify bval when making use
  840.      of any of the options in the openinfo parameter. This documentation error
  841.      will be fixed in the next release of Berkeley DB.
  842.  
  843.      That clarifies the situation with regards Berkeley DB itself. What about
  844.      DDDDBBBB____FFFFiiiilllleeee? Well, the behavior defined in the quote above is quite useful,
  845.      so DDDDBBBB____FFFFiiiilllleeee conforms it.
  846.  
  847.      That means that you can specify other options (e.g. cachesize) and still
  848.      have bval default to "\n" for variable length records, and space for
  849.      fixed length records.
  850.  
  851.  
  852.  
  853.  
  854.  
  855.                                                                        PPPPaaaaggggeeee 11113333
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  863.  
  864.  
  865.  
  866.      AAAA SSSSiiiimmmmpppplllleeee EEEExxxxaaaammmmpppplllleeee
  867.  
  868.      Here is a simple example that uses RECNO.
  869.  
  870.          use strict ;
  871.          use DB_File ;
  872.  
  873.          my @h ;
  874.          tie @h, "DB_File", "text", O_RDWR|O_CREAT, 0640, $DB_RECNO
  875.              or die "Cannot open file 'text': $!\n" ;
  876.  
  877.          # Add a few key/value pairs to the file
  878.          $h[0] = "orange" ;
  879.          $h[1] = "blue" ;
  880.          $h[2] = "yellow" ;
  881.  
  882.          # Check for existence of a key
  883.          print "Element 1 Exists with value $h[1]\n" if $h[1] ;
  884.  
  885.          # use a negative index
  886.          print "The last element is $h[-1]\n" ;
  887.          print "The 2nd last element is $h[-2]\n" ;
  888.  
  889.          untie @h ;
  890.  
  891.      Here is the output from the script:
  892.  
  893.          Element 1 Exists with value blue
  894.          The last element is yellow
  895.          The 2nd last element is blue
  896.  
  897.  
  898.      EEEExxxxttttrrrraaaa MMMMeeeetttthhhhooooddddssss
  899.  
  900.      As you can see from the example above, the tied array interface is quite
  901.      limited. To make the interface more useful, a number of methods are
  902.      supplied with DDDDBBBB____FFFFiiiilllleeee to simulate the standard array operations that are
  903.      not currently implemented in Perl's tied array interface. All these
  904.      methods are accessed via the object returned from the tie call.
  905.  
  906.      Here are the methods:
  907.  
  908.      $$$$XXXX---->>>>ppppuuuusssshhhh((((lllliiiisssstttt)))) ;;;;
  909.           Pushes the elements of list to the end of the array.
  910.  
  911.      $$$$vvvvaaaalllluuuueeee ==== $$$$XXXX---->>>>ppppoooopppp ;;;;
  912.           Removes and returns the last element of the array.
  913.  
  914.      $$$$XXXX---->>>>sssshhhhiiiifffftttt
  915.           Removes and returns the first element of the array.
  916.  
  917.  
  918.  
  919.  
  920.  
  921.                                                                        PPPPaaaaggggeeee 11114444
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  929.  
  930.  
  931.  
  932.      $$$$XXXX---->>>>uuuunnnnsssshhhhiiiifffftttt((((lllliiiisssstttt)))) ;;;;
  933.           Pushes the elements of list to the start of the array.
  934.  
  935.      $$$$XXXX---->>>>lllleeeennnnggggtttthhhh
  936.           Returns the number of elements in the array.
  937.  
  938.      AAAAnnnnooootttthhhheeeerrrr EEEExxxxaaaammmmpppplllleeee
  939.  
  940.      Here is a more complete example that makes use of some of the methods
  941.      described above. It also makes use of the API interface directly (see the
  942.      section on _T_H_E _A_P_I _I_N_T_E_R_F_A_C_E).
  943.  
  944.          use strict ;
  945.          use vars qw(@h $H $file $i) ;
  946.          use DB_File ;
  947.          use Fcntl ;
  948.  
  949.          $file = "text" ;
  950.  
  951.          unlink $file ;
  952.  
  953.          $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO
  954.              or die "Cannot open file $file: $!\n" ;
  955.  
  956.          # first create a text file to play with
  957.          $h[0] = "zero" ;
  958.          $h[1] = "one" ;
  959.          $h[2] = "two" ;
  960.          $h[3] = "three" ;
  961.          $h[4] = "four" ;
  962.  
  963.  
  964.          # Print the records in order.
  965.          #
  966.          # The length method is needed here because evaluating a tied
  967.          # array in a scalar context does not return the number of
  968.          # elements in the array.
  969.  
  970.          print "\nORIGINAL\n" ;
  971.          foreach $i (0 .. $H->length - 1) {
  972.              print "$i: $h[$i]\n" ;
  973.          }
  974.  
  975.          # use the push & pop methods
  976.          $a = $H->pop ;
  977.          $H->push("last") ;
  978.          print "\nThe last record was [$a]\n" ;
  979.  
  980.          # and the shift & unshift methods
  981.          $a = $H->shift ;
  982.          $H->unshift("first") ;
  983.          print "The first record was [$a]\n" ;
  984.  
  985.  
  986.  
  987.                                                                        PPPPaaaaggggeeee 11115555
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  995.  
  996.  
  997.  
  998.          # Use the API to add a new record after record 2.
  999.          $i = 2 ;
  1000.          $H->put($i, "Newbie", R_IAFTER) ;
  1001.  
  1002.          # and a new record before record 1.
  1003.          $i = 1 ;
  1004.          $H->put($i, "New One", R_IBEFORE) ;
  1005.  
  1006.          # delete record 3
  1007.          $H->del(3) ;
  1008.  
  1009.          # now print the records in reverse order
  1010.          print "\nREVERSE\n" ;
  1011.          for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
  1012.            { print "$i: $h[$i]\n" }
  1013.  
  1014.          # same again, but use the API functions instead
  1015.          print "\nREVERSE again\n" ;
  1016.          my ($s, $k, $v)  = (0, 0, 0) ;
  1017.          for ($s = $H->seq($k, $v, R_LAST) ;
  1018.                   $s == 0 ;
  1019.                   $s = $H->seq($k, $v, R_PREV))
  1020.            { print "$k: $v\n" }
  1021.  
  1022.          undef $H ;
  1023.          untie @h ;
  1024.  
  1025.      and this is what it outputs:
  1026.  
  1027.          ORIGINAL
  1028.          0: zero
  1029.          1: one
  1030.          2: two
  1031.          3: three
  1032.          4: four
  1033.  
  1034.          The last record was [four]
  1035.          The first record was [zero]
  1036.  
  1037.          REVERSE
  1038.          5: last
  1039.          4: three
  1040.          3: Newbie
  1041.          2: one
  1042.          1: New One
  1043.          0: first
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.                                                                        PPPPaaaaggggeeee 11116666
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  1061.  
  1062.  
  1063.  
  1064.          REVERSE again
  1065.          5: last
  1066.          4: three
  1067.          3: Newbie
  1068.          2: one
  1069.          1: New One
  1070.          0: first
  1071.  
  1072.      Notes:
  1073.  
  1074.      1.   Rather than iterating through the array, @h like this:
  1075.  
  1076.               foreach $i (@h)
  1077.  
  1078.           it is necessary to use either this:
  1079.  
  1080.               foreach $i (0 .. $H->length - 1)
  1081.  
  1082.           or this:
  1083.  
  1084.               for ($a = $H->get($k, $v, R_FIRST) ;
  1085.                    $a == 0 ;
  1086.                    $a = $H->get($k, $v, R_NEXT) )
  1087.  
  1088.  
  1089.      2.   Notice that both times the put method was used the record index was
  1090.           specified using a variable, $i, rather than the literal value
  1091.           itself. This is because put will return the record number of the
  1092.           inserted line via that parameter.
  1093.  
  1094. TTTTHHHHEEEE AAAAPPPPIIII IIIINNNNTTTTEEEERRRRFFFFAAAACCCCEEEE
  1095.      As well as accessing Berkeley DB using a tied hash or array, it is also
  1096.      possible to make direct use of most of the API functions defined in the
  1097.      Berkeley DB documentation.
  1098.  
  1099.      To do this you need to store a copy of the object returned from the tie.
  1100.  
  1101.              $db = tie %hash, "DB_File", "filename" ;
  1102.  
  1103.      Once you have done that, you can access the Berkeley DB API functions as
  1104.      DDDDBBBB____FFFFiiiilllleeee methods directly like this:
  1105.  
  1106.              $db->put($key, $value, R_NOOVERWRITE) ;
  1107.  
  1108.      IIIImmmmppppoooorrrrttttaaaannnntttt:::: If you have saved a copy of the object returned from tie, the
  1109.      underlying database file will _n_o_t be closed until both the tied variable
  1110.      is untied and all copies of the saved object are destroyed.
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.                                                                        PPPPaaaaggggeeee 11117777
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  1127.  
  1128.  
  1129.  
  1130.          use DB_File ;
  1131.          $db = tie %hash, "DB_File", "filename"
  1132.              or die "Cannot tie filename: $!" ;
  1133.          ...
  1134.          undef $db ;
  1135.          untie %hash ;
  1136.  
  1137.      See the section on _T_h_e _u_n_t_i_e() _G_o_t_c_h_a for more details.
  1138.  
  1139.      All the functions defined in the _d_b_o_p_e_n manpage are available except for
  1140.      _c_l_o_s_e() and _d_b_o_p_e_n() itself. The DDDDBBBB____FFFFiiiilllleeee method interface to the
  1141.      supported functions have been implemented to mirror the way Berkeley DB
  1142.      works whenever possible. In particular note that:
  1143.  
  1144.      +o    The methods return a status value. All return 0 on success.  All
  1145.           return -1 to signify an error and set $! to the exact error code.
  1146.           The return code 1 generally (but not always) means that the key
  1147.           specified did not exist in the database.
  1148.  
  1149.           Other return codes are defined. See below and in the Berkeley DB
  1150.           documentation for details. The Berkeley DB documentation should be
  1151.           used as the definitive source.
  1152.  
  1153.      +o    Whenever a Berkeley DB function returns data via one of its
  1154.           parameters, the equivalent DDDDBBBB____FFFFiiiilllleeee method does exactly the same.
  1155.  
  1156.      +o    If you are careful, it is possible to mix API calls with the tied
  1157.           hash/array interface in the same piece of code. Although only a few
  1158.           of the methods used to implement the tied interface currently make
  1159.           use of the cursor, you should always assume that the cursor has been
  1160.           changed any time the tied hash/array interface is used. As an
  1161.           example, this code will probably not do what you expect:
  1162.  
  1163.               $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE
  1164.                   or die "Cannot tie $filename: $!" ;
  1165.  
  1166.               # Get the first key/value pair and set  the cursor
  1167.               $X->seq($key, $value, R_FIRST) ;
  1168.  
  1169.               # this line will modify the cursor
  1170.               $count = scalar keys %x ;
  1171.  
  1172.               # Get the second key/value pair.
  1173.               # oops, it didn't, it got the last key/value pair!
  1174.               $X->seq($key, $value, R_NEXT) ;
  1175.  
  1176.           The code above can be rearranged to get around the problem, like
  1177.           this:
  1178.  
  1179.               $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE
  1180.                   or die "Cannot tie $filename: $!" ;
  1181.  
  1182.  
  1183.  
  1184.  
  1185.                                                                        PPPPaaaaggggeeee 11118888
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  1193.  
  1194.  
  1195.  
  1196.               # this line will modify the cursor
  1197.               $count = scalar keys %x ;
  1198.  
  1199.               # Get the first key/value pair and set  the cursor
  1200.               $X->seq($key, $value, R_FIRST) ;
  1201.  
  1202.               # Get the second key/value pair.
  1203.               # worked this time.
  1204.               $X->seq($key, $value, R_NEXT) ;
  1205.  
  1206.  
  1207.      All the constants defined in the _d_b_o_p_e_n manpage for use in the flags
  1208.      parameters in the methods defined below are also available. Refer to the
  1209.      Berkeley DB documentation for the precise meaning of the flags values.
  1210.  
  1211.      Below is a list of the methods available.
  1212.  
  1213.      $$$$ssssttttaaaattttuuuussss ==== $$$$XXXX---->>>>ggggeeeetttt(((($$$$kkkkeeeeyyyy,,,, $$$$vvvvaaaalllluuuueeee [[[[,,,, $$$$ffffllllaaaaggggssss]]]])))) ;;;;
  1214.           Given a key ($key) this method reads the value associated with it
  1215.           from the database. The value read from the database is returned in
  1216.           the $value parameter.
  1217.  
  1218.           If the key does not exist the method returns 1.
  1219.  
  1220.           No flags are currently defined for this method.
  1221.  
  1222.      $$$$ssssttttaaaattttuuuussss ==== $$$$XXXX---->>>>ppppuuuutttt(((($$$$kkkkeeeeyyyy,,,, $$$$vvvvaaaalllluuuueeee [[[[,,,, $$$$ffffllllaaaaggggssss]]]])))) ;;;;
  1223.           Stores the key/value pair in the database.
  1224.  
  1225.           If you use either the R_IAFTER or R_IBEFORE flags, the $key
  1226.           parameter will have the record number of the inserted key/value pair
  1227.           set.
  1228.  
  1229.           Valid flags are R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE and
  1230.           R_SETCURSOR.
  1231.  
  1232.      $$$$ssssttttaaaattttuuuussss ==== $$$$XXXX---->>>>ddddeeeellll(((($$$$kkkkeeeeyyyy [[[[,,,, $$$$ffffllllaaaaggggssss]]]])))) ;;;;
  1233.           Removes all key/value pairs with key $key from the database.
  1234.  
  1235.           A return code of 1 means that the requested key was not in the
  1236.           database.
  1237.  
  1238.           R_CURSOR is the only valid flag at present.
  1239.  
  1240.      $$$$ssssttttaaaattttuuuussss ==== $$$$XXXX---->>>>ffffdddd ;;;;
  1241.           Returns the file descriptor for the underlying database.
  1242.  
  1243.           See the section on _L_o_c_k_i_n_g _D_a_t_a_b_a_s_e_s for an example of how to make
  1244.           use of the fd method to lock your database.
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.                                                                        PPPPaaaaggggeeee 11119999
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  1259.  
  1260.  
  1261.  
  1262.      $$$$ssssttttaaaattttuuuussss ==== $$$$XXXX---->>>>sssseeeeqqqq(((($$$$kkkkeeeeyyyy,,,, $$$$vvvvaaaalllluuuueeee,,,, $$$$ffffllllaaaaggggssss)))) ;;;;
  1263.           This interface allows sequential retrieval from the database. See
  1264.           the _d_b_o_p_e_n manpage for full details.
  1265.  
  1266.           Both the $key and $value parameters will be set to the key/value
  1267.           pair read from the database.
  1268.  
  1269.           The flags parameter is mandatory. The valid flag values are
  1270.           R_CURSOR, R_FIRST, R_LAST, R_NEXT and R_PREV.
  1271.  
  1272.      $$$$ssssttttaaaattttuuuussss ==== $$$$XXXX---->>>>ssssyyyynnnncccc(((([[[[$$$$ffffllllaaaaggggssss]]]])))) ;;;;
  1273.           Flushes any cached buffers to disk.
  1274.  
  1275.           R_RECNOSYNC is the only valid flag at present.
  1276.  
  1277. HHHHIIIINNNNTTTTSSSS AAAANNNNDDDD TTTTIIIIPPPPSSSS
  1278.      LLLLoooocccckkkkiiiinnnngggg DDDDaaaattttaaaabbbbaaaasssseeeessss
  1279.  
  1280.      Concurrent access of a read-write database by several parties requires
  1281.      them all to use some kind of locking.  Here's an example of Tom's that
  1282.      uses the _f_d method to get the file descriptor, and then a careful _o_p_e_n()
  1283.      to give something Perl will _f_l_o_c_k() for you.  Run this repeatedly in the
  1284.      background to watch the locks granted in proper order.
  1285.  
  1286.          use DB_File;
  1287.  
  1288.          use strict;
  1289.  
  1290.          sub LOCK_SH { 1 }
  1291.          sub LOCK_EX { 2 }
  1292.          sub LOCK_NB { 4 }
  1293.          sub LOCK_UN { 8 }
  1294.  
  1295.          my($oldval, $fd, $db, %db, $value, $key);
  1296.  
  1297.          $key = shift || 'default';
  1298.          $value = shift || 'magic';
  1299.  
  1300.          $value .= " $$";
  1301.  
  1302.          $db = tie(%db, 'DB_File', '/tmp/foo.db', O_CREAT|O_RDWR, 0644)
  1303.                  || die "dbcreat /tmp/foo.db $!";
  1304.          $fd = $db->fd;
  1305.          print "$$: db fd is $fd\n";
  1306.          open(DB_FH, "+<&=$fd") || die "dup $!";
  1307.  
  1308.          unless (flock (DB_FH, LOCK_SH | LOCK_NB)) {
  1309.              print "$$: CONTENTION; can't read during write update!
  1310.                          Waiting for read lock ($!) ....";
  1311.              unless (flock (DB_FH, LOCK_SH)) { die "flock: $!" }
  1312.          }
  1313.          print "$$: Read lock granted\n";
  1314.  
  1315.  
  1316.  
  1317.                                                                        PPPPaaaaggggeeee 22220000
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  1325.  
  1326.  
  1327.  
  1328.          $oldval = $db{$key};
  1329.          print "$$: Old value was $oldval\n";
  1330.          flock(DB_FH, LOCK_UN);
  1331.  
  1332.          unless (flock (DB_FH, LOCK_EX | LOCK_NB)) {
  1333.              print "$$: CONTENTION; must have exclusive lock!
  1334.                          Waiting for write lock ($!) ....";
  1335.              unless (flock (DB_FH, LOCK_EX)) { die "flock: $!" }
  1336.          }
  1337.  
  1338.          print "$$: Write lock granted\n";
  1339.          $db{$key} = $value;
  1340.          $db->sync;  # to flush
  1341.          sleep 10;
  1342.  
  1343.          flock(DB_FH, LOCK_UN);
  1344.          undef $db;
  1345.          untie %db;
  1346.          close(DB_FH);
  1347.          print "$$: Updated db to $key=$value\n";
  1348.  
  1349.  
  1350.      SSSShhhhaaaarrrriiiinnnngggg DDDDaaaattttaaaabbbbaaaasssseeeessss WWWWiiiitttthhhh CCCC AAAApppppppplllliiiiccccaaaattttiiiioooonnnnssss
  1351.  
  1352.      There is no technical reason why a Berkeley DB database cannot be shared
  1353.      by both a Perl and a C application.
  1354.  
  1355.      The vast majority of problems that are reported in this area boil down to
  1356.      the fact that C strings are NULL terminated, whilst Perl strings are not.
  1357.  
  1358.      Here is a real example. Netscape 2.0 keeps a record of the locations you
  1359.      visit along with the time you last visited them in a DB_HASH database.
  1360.      This is usually stored in the file ~/._n_e_t_s_c_a_p_e/_h_i_s_t_o_r_y._d_b. The key field
  1361.      in the database is the location string and the value field is the time
  1362.      the location was last visited stored as a 4 byte binary value.
  1363.  
  1364.      If you haven't already guessed, the location string is stored with a
  1365.      terminating NULL. This means you need to be careful when accessing the
  1366.      database.
  1367.  
  1368.      Here is a snippet of code that is loosely based on Tom Christiansen's _g_g_h
  1369.      script (available from your nearest CPAN archive in
  1370.      _a_u_t_h_o_r_s/_i_d/_T_O_M_C/_s_c_r_i_p_t_s/_n_s_h_i_s_t._g_z).
  1371.  
  1372.          use strict ;
  1373.          use DB_File ;
  1374.          use Fcntl ;
  1375.  
  1376.          use vars qw( $dotdir $HISTORY %hist_db $href $binary_time $date ) ;
  1377.          $dotdir = $ENV{HOME} || $ENV{LOGNAME};
  1378.  
  1379.          $HISTORY = "$dotdir/.netscape/history.db";
  1380.  
  1381.  
  1382.  
  1383.                                                                        PPPPaaaaggggeeee 22221111
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  1391.  
  1392.  
  1393.  
  1394.          tie %hist_db, 'DB_File', $HISTORY
  1395.              or die "Cannot open $HISTORY: $!\n" ;;
  1396.  
  1397.          # Dump the complete database
  1398.          while ( ($href, $binary_time) = each %hist_db ) {
  1399.  
  1400.              # remove the terminating NULL
  1401.              $href =~ s/\x00$// ;
  1402.  
  1403.              # convert the binary time into a user friendly string
  1404.              $date = localtime unpack("V", $binary_time);
  1405.              print "$date $href\n" ;
  1406.          }
  1407.  
  1408.          # check for the existence of a specific key
  1409.          # remember to add the NULL
  1410.          if ( $binary_time = $hist_db{"http://mox.perl.com/\x00"} ) {
  1411.              $date = localtime unpack("V", $binary_time) ;
  1412.              print "Last visited mox.perl.com on $date\n" ;
  1413.          }
  1414.          else {
  1415.              print "Never visited mox.perl.com\n"
  1416.          }
  1417.  
  1418.          untie %hist_db ;
  1419.  
  1420.  
  1421.      TTTThhhheeee _u_n_t_i_e() Gotcha
  1422.  
  1423.      If you make use of the Berkeley DB API, it is _v_e_r_y strongly recommended
  1424.      that you read the section on _T_h_e _u_n_t_i_e _G_o_t_c_h_a in the _p_e_r_l_t_i_e manpage.
  1425.  
  1426.      Even if you don't currently make use of the API interface, it is still
  1427.      worth reading it.
  1428.  
  1429.      Here is an example which illustrates the problem from a DDDDBBBB____FFFFiiiilllleeee
  1430.      perspective:
  1431.  
  1432.          use DB_File ;
  1433.          use Fcntl ;
  1434.  
  1435.          my %x ;
  1436.          my $X ;
  1437.  
  1438.          $X = tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_TRUNC
  1439.              or die "Cannot tie first time: $!" ;
  1440.  
  1441.          $x{123} = 456 ;
  1442.  
  1443.          untie %x ;
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.                                                                        PPPPaaaaggggeeee 22222222
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  1457.  
  1458.  
  1459.  
  1460.          tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_CREAT
  1461.              or die "Cannot tie second time: $!" ;
  1462.  
  1463.          untie %x ;
  1464.  
  1465.      When run, the script will produce this error message:
  1466.  
  1467.          Cannot tie second time: Invalid argument at bad.file line 14.
  1468.  
  1469.      Although the error message above refers to the second _t_i_e() statement in
  1470.      the script, the source of the problem is really with the _u_n_t_i_e()
  1471.      statement that precedes it.
  1472.  
  1473.      Having read the _p_e_r_l_t_i_e manpage you will probably have already guessed
  1474.      that the error is caused by the extra copy of the tied object stored in
  1475.      $X.  If you haven't, then the problem boils down to the fact that the
  1476.      DDDDBBBB____FFFFiiiilllleeee destructor, DESTROY, will not be called until _a_l_l references to
  1477.      the tied object are destroyed. Both the tied variable, %x, and $X above
  1478.      hold a reference to the object. The call to _u_n_t_i_e() will destroy the
  1479.      first, but $X still holds a valid reference, so the destructor will not
  1480.      get called and the database file _t_s_t._f_i_l will remain open. The fact that
  1481.      Berkeley DB then reports the attempt to open a database that is alreday
  1482.      open via the catch-all "Invalid argument" doesn't help.
  1483.  
  1484.      If you run the script with the -w flag the error message becomes:
  1485.  
  1486.          untie attempted while 1 inner references still exist at bad.file line 12.
  1487.          Cannot tie second time: Invalid argument at bad.file line 14.
  1488.  
  1489.      which pinpoints the real problem. Finally the script can now be modified
  1490.      to fix the original problem by destroying the API object before the
  1491.      untie:
  1492.  
  1493.          ...
  1494.          $x{123} = 456 ;
  1495.  
  1496.          undef $X ;
  1497.          untie %x ;
  1498.  
  1499.          $X = tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_CREAT
  1500.          ...
  1501.  
  1502.  
  1503. CCCCOOOOMMMMMMMMOOOONNNN QQQQUUUUEEEESSSSTTTTIIIIOOOONNNNSSSS
  1504.      WWWWhhhhyyyy iiiissss tttthhhheeeerrrreeee PPPPeeeerrrrllll ssssoooouuuurrrrcccceeee iiiinnnn mmmmyyyy ddddaaaattttaaaabbbbaaaasssseeee????
  1505.  
  1506.      If you look at the contents of a database file created by DB_File, there
  1507.      can sometimes be part of a Perl script included in it.
  1508.  
  1509.      This happens because Berkeley DB uses dynamic memory to allocate buffers
  1510.      which will subsequently be written to the database file. Being dynamic,
  1511.      the memory could have been used for anything before DB malloced it. As
  1512.  
  1513.  
  1514.  
  1515.                                                                        PPPPaaaaggggeeee 22223333
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  1523.  
  1524.  
  1525.  
  1526.      Berkeley DB doesn't clear the memory once it has been allocated, the
  1527.      unused portions will contain random junk. In the case where a Perl script
  1528.      gets written to the database, the random junk will correspond to an area
  1529.      of dynamic memory that happened to be used during the compilation of the
  1530.      script.
  1531.  
  1532.      Unless you don't like the possibility of there being part of your Perl
  1533.      scripts embedded in a database file, this is nothing to worry about.
  1534.  
  1535.      HHHHoooowwww ddddoooo IIII ssssttttoooorrrreeee ccccoooommmmpppplllleeeexxxx ddddaaaattttaaaa ssssttttrrrruuuuccccttttuuuurrrreeeessss wwwwiiiitttthhhh DDDDBBBB____FFFFiiiilllleeee????
  1536.  
  1537.      Although DDDDBBBB____FFFFiiiilllleeee cannot do this directly, there is a module which can
  1538.      layer transparently over DDDDBBBB____FFFFiiiilllleeee to accomplish this feat.
  1539.  
  1540.      Check out the MLDBM module, available on CPAN in the directory
  1541.      _m_o_d_u_l_e_s/_b_y-_m_o_d_u_l_e/_M_L_D_B_M.
  1542.  
  1543.      WWWWhhhhaaaatttt ddddooooeeeessss """"IIIInnnnvvvvaaaalllliiiidddd AAAArrrrgggguuuummmmeeeennnntttt"""" mmmmeeeeaaaannnn????
  1544.  
  1545.      You will get this error message when one of the parameters in the tie
  1546.      call is wrong. Unfortunately there are quite a few parameters to get
  1547.      wrong, so it can be difficult to figure out which one it is.
  1548.  
  1549.      Here are a couple of possibilities:
  1550.  
  1551.      1.   Attempting to reopen a database without closing it.
  1552.  
  1553.      2.   Using the O_WRONLY flag.
  1554.  
  1555.      WWWWhhhhaaaatttt ddddooooeeeessss """"BBBBaaaarrrreeeewwwwoooorrrrdddd ''''DDDDBBBB____FFFFiiiilllleeee'''' nnnnooootttt aaaalllllllloooowwwweeeedddd"""" mmmmeeeeaaaannnn????
  1556.  
  1557.      You will encounter this particular error message when you have the strict
  1558.      'subs' pragma (or the full strict pragma) in your script.  Consider this
  1559.      script:
  1560.  
  1561.          use strict ;
  1562.          use DB_File ;
  1563.          use vars qw(%x) ;
  1564.          tie %x, DB_File, "filename" ;
  1565.  
  1566.      Running it produces the error in question:
  1567.  
  1568.          Bareword "DB_File" not allowed while "strict subs" in use
  1569.  
  1570.      To get around the error, place the word DB_File in either single or
  1571.      double quotes, like this:
  1572.  
  1573.          tie %x, "DB_File", "filename" ;
  1574.  
  1575.      Although it might seem like a real pain, it is really worth the effort of
  1576.      having a use strict in all your scripts.
  1577.  
  1578.  
  1579.  
  1580.  
  1581.                                                                        PPPPaaaaggggeeee 22224444
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  1589.  
  1590.  
  1591.  
  1592. HHHHIIIISSSSTTTTOOOORRRRYYYY
  1593.      Moved to the Changes file.
  1594.  
  1595. BBBBUUUUGGGGSSSS
  1596.      Some older versions of Berkeley DB had problems with fixed length records
  1597.      using the RECNO file format. This problem has been fixed since version
  1598.      1.85 of Berkeley DB.
  1599.  
  1600.      I am sure there are bugs in the code. If you do find any, or can suggest
  1601.      any enhancements, I would welcome your comments.
  1602.  
  1603. AAAAVVVVAAAAIIIILLLLAAAABBBBIIIILLLLIIIITTTTYYYY
  1604.      DDDDBBBB____FFFFiiiilllleeee comes with the standard Perl source distribution. Look in the
  1605.      directory _e_x_t/_D_B__F_i_l_e. Given the amount of time between releases of Perl
  1606.      the version that ships with Perl is quite likely to be out of date, so
  1607.      the most recent version can always be found on CPAN (see the CPAN entry
  1608.      in the _p_e_r_l_m_o_d manpage for details), in the directory _m_o_d_u_l_e_s/_b_y-
  1609.      _m_o_d_u_l_e/_D_B__F_i_l_e.
  1610.  
  1611.      This version of DDDDBBBB____FFFFiiiilllleeee will work with either version 1.x or 2.x of
  1612.      Berkeley DB, but is limited to the functionality provided by version 1.
  1613.  
  1614.      The official web site for Berkeley DB is _h_t_t_p://_w_w_w._s_l_e_e_p_y_c_a_t._c_o_m/_d_b. The
  1615.      ftp equivalent is _f_t_p._s_l_e_e_p_y_c_a_t._c_o_m:/_p_u_b. Both versions 1 and 2 of
  1616.      Berkeley DB are available there.
  1617.  
  1618.      Alternatively, Berkeley DB version 1 is available at your nearest CPAN
  1619.      archive in _s_r_c/_m_i_s_c/_d_b._1._8_5._t_a_r._g_z.
  1620.  
  1621.      If you are running IRIX, then get Berkeley DB version 1 from
  1622.      _h_t_t_p://_r_e_a_l_i_t_y._s_g_i._c_o_m/_a_r_i_e_l. It has the patches necessary to compile
  1623.      properly on IRIX 5.3.
  1624.  
  1625. CCCCOOOOPPPPYYYYRRRRIIIIGGGGHHHHTTTT
  1626.      Copyright (c) 1997 Paul Marquess. All rights reserved. This program is
  1627.      free software; you can redistribute it and/or modify it under the same
  1628.      terms as Perl itself.
  1629.  
  1630.      Although DDDDBBBB____FFFFiiiilllleeee is covered by the Perl license, the library it makes use
  1631.      of, namely Berkeley DB, is not. Berkeley DB has its own copyright and its
  1632.      own license. Please take the time to read it.
  1633.  
  1634.      The license for Berkeley DB version 2, and how it relates to DB_File does
  1635.      need some extra clarification. Here are are few words taken from the
  1636.      Berkeley DB FAQ regarding the version 2 license:
  1637.  
  1638.          The major difference is that the license for DB 2.0, when
  1639.          downloaded from the net, requires that the software that
  1640.          uses DB 2.0 be freely redistributable.
  1641.  
  1642.      That means that if you want to use DB_File, and you have changed either
  1643.      the source for Berkeley DB or Perl, then the changes must be freely
  1644.  
  1645.  
  1646.  
  1647.                                                                        PPPPaaaaggggeeee 22225555
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  1655.  
  1656.  
  1657.  
  1658.      available.
  1659.  
  1660.      In the case of Perl, the term source refers to the complete source code
  1661.      for Perl (e.g. sv.c, toke.c, perl.h) and any external modules that you
  1662.      are using (e.g. DB_File, Tk).
  1663.  
  1664.      Note that any Perl scripts that you write are your property - this
  1665.      includes scripts that make use of DB_File. Neither the Perl license or
  1666.      the Berkeley DB license place any restriction on what you have to do with
  1667.      them.
  1668.  
  1669.      If you are in any doubt about the license situation, contact either the
  1670.      Berkeley DB authors or the author of DB_File. See the section on _A_U_T_H_O_R
  1671.      for details.
  1672.  
  1673. SSSSEEEEEEEE AAAALLLLSSSSOOOO
  1674.      the _p_e_r_l(_1) manpage, the _d_b_o_p_e_n(_3) manpage, the _h_a_s_h(_3) manpage, the
  1675.      _r_e_c_n_o(_3) manpage, the _b_t_r_e_e(_3) manpage
  1676.  
  1677. AAAAUUUUTTTTHHHHOOOORRRR
  1678.      The DB_File interface was written by Paul Marquess
  1679.      <pmarquess@bfsec.bt.co.uk>.  Questions about the DB system itself may be
  1680.      addressed to <db@sleepycat.com<gt>.
  1681.  
  1682.  
  1683.  
  1684.  
  1685.  
  1686.  
  1687.  
  1688.  
  1689.  
  1690.  
  1691.  
  1692.  
  1693.  
  1694.  
  1695.  
  1696.  
  1697.  
  1698.  
  1699.  
  1700.  
  1701.  
  1702.  
  1703.  
  1704.  
  1705.  
  1706.  
  1707.  
  1708.  
  1709.  
  1710.  
  1711.  
  1712.  
  1713.                                                                        PPPPaaaaggggeeee 22226666
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720. DDDDBBBB____FFFFiiiilllleeee((((3333))))                                                          DDDDBBBB____FFFFiiiilllleeee((((3333))))
  1721.  
  1722.  
  1723.  
  1724.  
  1725.  
  1726.  
  1727.  
  1728.  
  1729.  
  1730.  
  1731.  
  1732.  
  1733.  
  1734.  
  1735.  
  1736.  
  1737.  
  1738.  
  1739.  
  1740.  
  1741.  
  1742.  
  1743.  
  1744.  
  1745.  
  1746.  
  1747.  
  1748.  
  1749.  
  1750.  
  1751.  
  1752.  
  1753.  
  1754.  
  1755.  
  1756.  
  1757.  
  1758.  
  1759.  
  1760.  
  1761.  
  1762.  
  1763.  
  1764.  
  1765.  
  1766.  
  1767.  
  1768.  
  1769.  
  1770.  
  1771.  
  1772.  
  1773.  
  1774.  
  1775.  
  1776.                                                                        PPPPaaaaggggeeee 22227777
  1777.  
  1778.  
  1779.  
  1780.  
  1781.  
  1782.  
  1783.